package com.simple.datax.recv.impl;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.SimpleMessage;
import com.simple.datax.api.MessageProcessor;
import com.simple.datax.api.SimpleException;
import com.simple.datax.core.model.connpara.CommonConnPara;

public class ActiveMQMessageListener implements MessageListener {
	private static final Logger logger = LoggerFactory
			.getLogger(ActiveMQMessageListener.class);
	private MessageProcessor nextProcessor;
	private CommonConnPara connPara;
	@Override
	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage txtMsg = (TextMessage) message;
				String msg = txtMsg.getText();
				logger.debug("Consumer:->Received: " + msg);
				try {
					Thread.sleep(2000);
				} catch (Exception ex) {
					
				}
				logger.debug("Consumer:->Received: 1111 " + msg);
				nextProcessor.process(createSimpleMessage());
			} else {
				logger.debug("Consumer:->Received: " + message);
			}
		} catch (JMSException e) {
			logger.error("onMessage", e);
		} catch (SimpleException e) {
			logger.error("onMessage", e);
		} catch (Exception e) {
			logger.error("onMessage", e);
		}
	}

	private SimpleMessage createSimpleMessage() {
		SimpleMessage ret = new SimpleMessage();
		ret.setSentCommunParam(connPara.getTransportPara());
		ret.setSentCommunType(connPara.getTransportType());
		return ret;
	}

	public CommonConnPara getConnPara() {
		return connPara;
	}

	public void setConnPara(CommonConnPara connPara) {
		this.connPara = connPara;
	}

	public MessageProcessor getNextProcessor() {
		return nextProcessor;
	}

	public void setNextProcessor(MessageProcessor nextProcessor) {
		this.nextProcessor = nextProcessor;
	}
	
	
}
